package com.android.asportoonline;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ElencoLocaliActivity extends ListActivity {

	private static final String SOAP_ACTION = null;
	private static final String IP = "10.0.2.2";
	private static final String URL = "http://" + IP
			+ ":8080/AsportoOnlineWS/AsportoOnlineWS?WSDL";
	private static final String NAMESPACE = "http://beans.business.asportoonline.org/";

	public List<String> recuperaLocali(String localita) {
		SoapObject req = new SoapObject(NAMESPACE, "recuperaLocali");
		req.addProperty("localita", localita);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);
		envelope.setOutputSoapObject(req);
		HttpTransportSE transport = new HttpTransportSE(URL);
		transport.debug = true;
		Object res = null;
		List<String> listaLocali = new ArrayList<String>();

		try {
			transport.call(SOAP_ACTION, envelope);

			System.out.println("XML -> " + transport.responseDump);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}

		try {
			res = envelope.getResponse();
		} catch (SoapFault e) {
			e.printStackTrace();
		}

		if (res != null) {
			try {
				listaLocali = (List<String>) res;
			} catch (ClassCastException e) {
				listaLocali.add(res.toString());
			}
		}

		return listaLocali;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.enableDefaults();
		super.onCreate(savedInstanceState);

		String localita = getIntent().getExtras().getString(
				getPackageName() + ".localita");

		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, recuperaLocali(localita)));
	}

	public void onListItemClick(ListView parent, View v, int position, long id) {
		Intent intent = new Intent("com.android.DETTAGLILOCALEACTIVITY");

		intent.putExtra(getPackageName() + ".locale", parent
				.getItemAtPosition(position).toString());

		startActivity(intent);
	}


}